#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 110;
int n,k;
int x[maxn],y[maxn];
int aim[maxn][5];
int ans[5];
int vis[maxn]={0};
int cnt;
int judge(int *ans)
{
	
	for(int m = 1;m<=k;m++)
	{
	//	printf("********\n");
		int x1 = 0,y1 = 0;
		for(int i = 1;i<=4;i++) 
		{
			if(aim[m][i]==ans[i]) x1++;
			else
			{
				for(int j = 1;j<=4;j++)
				{
					if(aim[m][i]==ans[j]) y1++;
				}
			}
		}
		if(x1!=x[m]||y1!=y[m]) return 0;
	}
	return 1;
}
void dfs(int pos,int *ans)
{
	if(pos==5)
	{
		if(judge(ans)) printf("%d %d %d %d\n",ans[1],ans[2],ans[3],ans[4]),	cnt++;
		return ;
	}
	for(int i = 1;i<=n;i++)
	{
		if(!vis[i])
		{
			vis[i] = 1;
			ans[pos] = i;
			dfs(pos+1,ans);
			vis[i] = 0;
		}
	}
}
int main()
{
	while(scanf("%d",&n)&&n!=0)
	{
		scanf("%d",&k);
			for(int i = 1;i<=k;i++)
				scanf("%d %d %d %d %d %d",&aim[i][1],&aim[i][2],&aim[i][3],&aim[i][4],&x[i],&y[i]);
			cnt = 0;
			dfs(1,ans);
			printf("%d\n",cnt);
		
	}
} 
